package com.shakev.coupon.web.model.base;


import java.io.Serializable;

public class RestResponse<T> implements Serializable {


	private static final long serialVersionUID = -2571222031261651786L;

	/** 成功或失败 */
	public boolean flag=true;

	/** 错误代码 */
	public int code=0;

	/** 消息文本 */
	public String message="成功";

	/** 消息数据 */
	public T data;

	public RestResponse() {
	}

	public static final RestResponse success() {
		RestResponse response = new RestResponse();
		response.setFlag(true);
		response.setCode(0);
		response.setMessage("成功");
		response.setData(null);
		return response;
	}

	public static<T>  RestResponse<T> success(String message,T t) {
		RestResponse response = new RestResponse();
		response.setFlag(true);
		response.setCode(0);
		response.setMessage(message);
		response.setData(t);
		return response;
	}
	public static<T>  RestResponse<T> success(int code, String message,T t) {
		RestResponse response = new RestResponse();
		response.setFlag(true);
		response.setCode(code);
		response.setMessage(message);
		response.setData(t);
		return response;
	}

	public static<T>  RestResponse<T> success(T t) {
		RestResponse response = new RestResponse();
		response.setFlag(true);
		response.setCode(0);
		response.setMessage("成功");
		response.setData(t);
		return response;
	}

	public static final RestResponse failure(String message) {
		RestResponse response = new RestResponse();
		response.setFlag(false);
		response.setCode(GenericCode.ERROR.getCode());
		response.setMessage(message);
		response.setData(null);
		return response;
	}

	public static final RestResponse paramFailure(String message){
		RestResponse response = new RestResponse();
		response.setFlag(false);
		response.setCode(GenericCode.PARAM_ERROR.getCode());
		response.setMessage(message);
		response.setData(null);
		return response;
	}

	public static final RestResponse failure(int code, String message) {
		RestResponse response = new RestResponse();
		response.setFlag(false);
		response.setCode(code);
		response.setMessage(message);
		response.setData(null);
		return response;
	}

	public static final RestResponse failure(Exception e) {
		RestResponse response = new RestResponse();
		response.setFlag(false);
		response.setCode(GenericCode.ERROR.getCode());
		response.setMessage(e.getMessage());
		response.setData(null);
		return response;
	}

	public static<T>  RestResponse<T> failure(Exception e,T data) {
		RestResponse response = new RestResponse();
		response.setFlag(false);
		response.setCode(GenericCode.ERROR.getCode());
		response.setMessage(e.getMessage());
		response.setData(data);
		return response;
	}

	public static<T>  RestResponse<T> failure(T data) {
		RestResponse response = new RestResponse();
		response.setFlag(false);
		response.setCode(GenericCode.ERROR.getCode());
		response.setMessage(GenericCode.ERROR.getMessage());
		response.setData(data);
		return response;
	}

	public static final RestResponse failure(ErrorEntity error) {
		RestResponse response = new RestResponse();
		response.setFlag(false);
		response.setCode(error.getCode());
		response.setMessage(error.getMessage());
		response.setData(null);
		return response;
	}

	public static<T>  RestResponse<T> failure(ErrorEntity error,T data) {
		RestResponse response = new RestResponse();
		response.setFlag(false);
		response.setCode(error.getCode());
		response.setMessage(error.getMessage());
		response.setData(data);
		return response;
	}

	public static<T>  RestResponse<T> failure(Integer errorCode,String errorMessage,T data) {
		RestResponse response = new RestResponse();
		response.setFlag(false);
		response.setCode(errorCode);
		response.setMessage(errorMessage);
		response.setData(data);
		return response;
	}

	public static<T>  RestResponse<T> failure(ErrorEntity error,String errorMessage,T data) {
		RestResponse response = new RestResponse();
		response.setFlag(false);
		response.setCode(error.getCode());
		response.setMessage(errorMessage);
		response.setData(data);
		return response;
	}
	public boolean isFlag() {
		return flag;
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
	}

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

	@Override
	public String toString() {
		return "RestResponse [flag=" + flag + ", code=" + code + ", message=" + message + ", data=" + data + "]";
	}

}
